package it.polimi.travelDream.ejb.beans;

import java.util.List;

import it.polimi.travelDream.ejb.Cthulhu.Persistenza;
import it.polimi.travelDream.ejb.interfaces.PersonalizedMgrRemote;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import it.polimi.travelDream.entities.Hotel;
import it.polimi.travelDream.entities.Modified_Package;
import it.polimi.travelDream.entities.Package;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;


/**
 * Session Bean implementation class Personalized
 */
@Stateless
@LocalBean
public class PersonalizedMgr implements PersonalizedMgrRemote {

	Package pack;
	Persistenza persist=new Persistenza();
	public EntityManagerFactory factory;
	public EntityManager em;
	
    public Package getPack() {
		return pack;
	}

	public void setPack(Package pack) {
		this.pack = pack;
	}
    
	/**
	 * @author Filipazzi, Crisciullo
	 * This method adds a modified package to the user's control panel
	 */

	@Override
	public Modified_Package addpp(Modified_Package pp) {
		persist.persista(pp);
		pp.getExcursion().size();
		return pp;
	}
	
	
	/**
	 * @author Filipazzi, Crisciullo
	 * This method shows all the packages modified by the user
	 */

	public List<Modified_Package> showAllModifiedPackages(){
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
		em=factory.createEntityManager();
		
		TypedQuery<Modified_Package> query = em.createQuery("SELECT mp FROM Modified_Package mp", Modified_Package.class);
		
		List<Modified_Package> results = query.getResultList();
		
		em.close();
		factory.close();
		return results;
	}
	
	public Modified_Package getPersonalizedPackage(int id){
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
		em=factory.createEntityManager();
		Modified_Package personalizedpack = em.find(Modified_Package.class, id);
		em.close();
		factory.close();
		return personalizedpack;
	}

	
	/**
	 * @author Filipazzi, Crisciullo
	 * This method deletes all the packages modified by the user
	 */
	
		public void delPersonalizedPackage(int id){
				factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
				em=factory.createEntityManager();
			  Modified_Package pp = em.find(Modified_Package.class, id);
			  em.getTransaction().begin();
			  em.remove(pp);
			  em.getTransaction().commit();
				em.close();
				factory.close();
		}
}
